/*  Copyright (c) 2004-2007 Srichand Pendyala <srichand.pendyala@gmail.com>
 *  scoreclass.cs - This file is part of Sharpscore.
 *
 *  Sharpscore is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Sharpscore is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Sharpscore; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/*
 * Sample reply from the Rediff website:
 * 
 * l1=West Indies  vs Australia
 * l2=West Indies 
 * interval=1
 * message=West Indies  201-10 (34.3)
 * tagline=F Edwards(2) 
 * date=12 September, 2006
 * ver=15
 *
 * Use the above for reference only. Check with the real content (and update!)
 * if in doubt.
 */

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Data;

namespace Scorethingy
{
	/// <summary>
	/// This is the meat of the whole thing.
	/// Initializing a Score Object will automatically fetch the latest score from the website.
	/// PrintScore() will return a nice String which will display well in a GTK Label.
	/// 
	/// As simple as that. :)
	/// </summary>
	public class Score{
	        public string Match, NowPlaying, FinalScore, date;
		public void GetScore(){
			string  sc;
			string[] lines;
	            	WebClient myWebClient = new WebClient();
			string uriString;
			Stream myStream;
			StreamReader sr;
	            	try {
				uriString = "http://livechat.rediff.com:80/sports/score/score.txt";
	        	   	myStream = myWebClient.OpenRead(uriString);
				sr = new StreamReader(myStream);
		            	myStream.Close();
			}
			catch {
				throw new ScoreException("Connection Error!");
			}
			
		      	sc = sr.ReadToEnd();
			lines = sc.Split('\n');
			foreach(string line in lines){
				string[] b = line.Split('=');
				if(line.StartsWith("l1"))
					Match = b[1]; 
				else if(line.StartsWith("l2"))
					NowPlaying = b[1];
				else if(line.StartsWith("message"))
					FinalScore = b[1];
				else if(line.StartsWith("date"))
					date = b[1];
			}
		}
		public string PrintScore(){
			string fs;
			fs = "Match: "+Match+"\n";
			fs += "Now Playing: "+NowPlaying+"\n";
			fs += "Score: "+FinalScore+"\n";
			fs += "Date: "+date;
			return fs;
		}
		public Score(){
			this.GetScore();
		}
			
		
	}
	
	/// <summary>
	/// Highly unnecessary, but its there anyway!
	/// </summary>
	public class ScoreException:Exception{
		public ScoreException(string str){}
	}
							
}
